package com.qau.zek.controller;

import com.qau.zek.pojo.goods;
import com.qau.zek.pojo.shops;
import com.qau.zek.utils.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/shops")
public class ShopServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // JDBC
        System.out.println("ShopServlet.service()");
        //解决post请求中文乱码问题
        request.setCharacterEncoding("utf-8");
        String method = request.getParameter("method");
        switch (method) {
            case "add_shops":
                add_shops(request, response);
                break;
            case "select_shops":
                select_shops(request, response);
                break;
            case "deleteById":
                deleteById(request, response);
                break;
            case "selectById":
                selectById(request, response);
                break;
            case "update":
                update(request, response);
                break;
            default:
                break;
        }
    }

    private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("ShopsServlet.update()");
        String shop_id = request.getParameter("shop_id");
        String shop_name = request.getParameter("shop_name");
        String shop_loc = request.getParameter("shop_loc");
//        String shop_price = request.getParameter("shop_price");
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "update shops set shop_name=?,shop_loc=? where shop_id=?";
            statement = connection.prepareStatement(sql);
            statement.setString(3, shop_id);
            statement.setString(1, shop_name);
            statement.setString(2, shop_loc);
//            statement.setString(3,shop_price);
            System.out.println(statement);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        response.sendRedirect(request.getContextPath() + "/shops?method=select_shops");
    }

    private void selectById(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        System.out.println("shopServlet.selectById()");
        String shop_id = request.getParameter("shop_id");
        System.out.println(shop_id);
        List<shops> list = new ArrayList<shops>();
        try {
            Connection connection = null;
            PreparedStatement statement = null;
            connection = JDBCUtil.getConnection();
            String sql = "select * from shops where shop_id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, Integer.parseInt(shop_id));
            System.out.println(statement);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                int shop_id1 = resultSet.getInt("shop_id");
                String shop_name = resultSet.getString("shop_name");
                String shop_loc = resultSet.getString("shop_loc");
//                double shop_price = resultSet.getDouble("shop_price");
                shops shop = new shops(shop_id1, shop_name, shop_loc);
                request.setAttribute("shop", shop);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        for (shops shop : list) {
            System.out.println(shop);
        }

        request.getRequestDispatcher("/shops_update.jsp").forward(request, response);


    }

    private void add_shops(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("ShopServlet.add()");
        String shop_id = request.getParameter("shop_id");
        String shop_name = request.getParameter("shop_name");
        String shop_loc = request.getParameter("shop_loc");

        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "insert into shops(shop_id,shop_name,shop_loc) values(?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, shop_id);
            statement.setString(2, shop_name);
            statement.setString(3, shop_loc);
            System.out.println(statement);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //删除完之后，重定向到查找所有界面
        response.sendRedirect(request.getContextPath() + "/shops?method=select_shops");
    }

    private void select_shops(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<shops> list = new ArrayList<shops>();
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select shop_id,shop_name,shop_loc from shops";
            statement = connection.prepareStatement(sql);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                //每遍历一次查出一个User
                int shop_id = resultSet.getInt("shop_id");
                String shop_name = resultSet.getString("shop_name");
                String shop_loc = resultSet.getString("shop_loc");


                shops shop = new shops(shop_id, shop_name, shop_loc);
                list.add(shop);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        for (shops shop : list) {
            System.out.println(shop);
        }

        //把查找出来的数据放到req域对象里面（一块内存区域）
        request.setAttribute("shop_list", list);
        //转发到user_list.jsp页面进行展示
        request.getRequestDispatcher("/shops_list.jsp").forward(request, response);
    }

    private void deleteById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("shopServlet.deleteById()");
        String shop_id = request.getParameter("shop_id");
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "delete from shops where shop_id = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, Integer.parseInt(shop_id));
            System.out.println(preparedStatement);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        response.sendRedirect(request.getContextPath() + "/shops?method=select_shops");
    }

}
